package com.lz.football.module.goldpalace
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.events.GoldPalaceEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	
	import common.base.GoodsUsePanel;
	import common.item.ComGoodsItem;
	import common.item.SimplePlayerItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;
	import lz.controls.events.ListEvent;
	import lz.controls.managers.PopUpManager;

	/**
	 *黄金12宫 
	 * @author lake
	 * 
	 */	
	public class GoldPalaceView extends TitleWindow implements IGoldPalaceView
	{
		/**
		 *领取奖励 
		 */		
		private var getBtn 		:SimpleButton ;
		/**
		 *选择奖励 
		 */		
		private var changeBtn 		:SimpleButton;
		/**
		 *领取奖励剩余时间 
		 */		
		private var txtAwardTime	:TextField;
		/**
		 *显示奖励mc 
		 */		
		private var itemMc			:MovieClip;
		/**
		 *星座mc 
		 */		
		private var starMc			:MovieClip;
		/**
		 *动画mc 
		 */		
		private var motionMc		:MovieClip;
		/**
		 *全部点亮动画 
		 */		
		private var finishMT		:MovieClip;
		/**
		 *背景mc 
		 */		
		private var bgMc			:MovieClip;
		/**
		 *条目list 
		 */		
		private var itemlist :Array = [];
		/**
		 *奖励面板 
		 */		
		private var awardPanel 	:AwardPanel;
		/**
		 *奖励item 
		 */		
		private var comGoodItem 	:ComGoodsItem;
		/**
		 *选择的商品 
		 */		
		private var selectGoods 	:GoodsVo;
		/**
		 *选择的位置 
		 */		
		private var selectPos		:int;
		
		public function GoldPalaceView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.football.goldpalace.viewSkin");
			_symbolSkin.cacheAsBitmap = true;
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			windowBg.mouseChildren = false;
			getBtn = _symbolSkin.getBtn;
			getBtn.visible = false;
			changeBtn = _symbolSkin.changeBtn;
			txtAwardTime = _symbolSkin.txtAwardTime;
			txtAwardTime.text = "";
			itemMc = _symbolSkin.itemMc;
			starMc = _symbolSkin.starMc;
			motionMc = _symbolSkin.motionMc;
			motionMc.mouseChildren = false;
			motionMc.mouseEnabled = false;
			bgMc = _symbolSkin.bgMc;
			bgMc.mouseChildren = false;
			bgMc.mouseEnabled = false;
			finishMT = _symbolSkin.finishMT;
			finishMT.mouseChildren = false;
			finishMT.mouseEnabled = false;
			finishMT.visible = false;
			if(finishMT.parent)
			{
				finishMT.parent.removeChild(finishMT);
			}
			//设置可拖动
			dragEnabled = true;
			this.addChild(_symbolSkin);
			closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
			this.addChild(closeBtn);
			closeBtn.x = 707;
			closeBtn.y = 30;
			//设置关闭按钮
			setCloseBtn(closeBtn);
			//award panel
			awardPanel = new AwardPanel();
			awardPanel.initUI();
			//init
			var awardPos :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos;
			if(awardPos > 0)
			{
				initRes();
			}
			//add event
			addEvtListener();
		}
		/**
		 *注册事件 
		 */
		public function addEvtListener():void
		{
			changeBtn.addEventListener(MouseEvent.CLICK,changeAwardH);
			txtAwardTime.addEventListener(TextEvent.LINK,dropTimeH);
			getBtn.addEventListener(MouseEvent.CLICK,getAwardH);
		}
		/**
		 *初始化资源 
		 */		
		private function initRes():void
		{
			var active :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.active;
			var actives :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.actives;
			var awardPos :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos;
			var times :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.times;
			awardPanel.initRes();
			//初始化数据
			var i :int = 0;
			var pItem :SimplePlayerItem;
			var pSkin :Sprite;
			var pGoods :GoodsVo;
			var starIds :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.starIds;
			for(;i < 12;i++)
			{
				pItem = new SimplePlayerItem();
				pItem.param = (i+1);
				pItem.initUI();
				itemlist.push(pItem);
				pSkin = _symbolSkin["player_" + i];
				if(i == 0)
				{
					pSkin.alpha = 1;
					pItem.data = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();
				}
				else
				{
					pGoods = AppData.getInstance().goodsManager.getStarById(starIds[i-1]);
					pItem.data = pGoods;
				}
				DisplayObjectUtil.removeAllChildren(pSkin);
				pSkin.addChild(pItem);
			}
			var mSkin :MovieClip;
			for(i = 1;i<12;i++)
			{
				pSkin = _symbolSkin["player_" + i];
				mSkin = motionMc["acM_"+i];
				mSkin.visible = false;
				if(actives[i-1] == 1)
				{
					pSkin.alpha = 1;
					starMc["pMc_" + i].gotoAndStop(2);
				}
				else
				{
					pSkin.buttonMode = true;
					pSkin.addEventListener(ListEvent.ITEM_CLICK,pItemClickH);
				}
			}
			DisplayObjectUtil.removeAllChildren(itemMc.container);
			var awards :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.awards;
			if(comGoodItem == null)
			{
				comGoodItem = new ComGoodsItem();
				comGoodItem.initUI();
				comGoodItem.bg.visible = false;
				comGoodItem.x = -2;
				comGoodItem.y = -3;
			}
			if(awardPos > 0)
			{
				comGoodItem.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(awards[awardPos-1].type,awards[awardPos-1].id);
				refreshTime();
			}
			else
			{
				getBtn.visible = false;
				txtAwardTime.visible = false;
			}
			itemMc.container.addChild(comGoodItem);
			if(active == 12)
			{
				showGoldBg();
			}
		}
		/**
		 *显示金色背景 
		 */		
		private function showGoldBg():void
		{
			if(bgMc)
			{
				bgMc.gotoAndStop(2);
			}
		}
		/**
		 *计时 
		 */		
		private function timing():void
		{
			refreshTime();
		}
		/**
		 *刷新时间 
		 */		
		private function refreshTime():void
		{
			var curAwardIndex :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos;
			if(curAwardIndex <= 0)
			{
				curAwardIndex = 1;
			}
			var times :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.times;
			//s
			var begintime 	:int = AppData.getInstance().goldPalaceManager.goldPalaceVo.begintime;//开始计时时间
			var cdtime 		:int = times[curAwardIndex - 1]*24*60*60;//冷却时间
			var curtime 	:int = AppData.getInstance().time;//当前时间
			var passtime	:int = curtime - begintime;//经过的时间
			var lefttime	:int = cdtime - passtime;//剩下时间
			if(lefttime < 0 )
			{
				getBtn.visible = true;
				txtAwardTime.visible = false;
				TimerManager.instance.removeFunction(timing);
			}
			else
			{
				getBtn.visible = false;
				txtAwardTime.visible = true;
				txtAwardTime.htmlText = "<a href='event:"+curAwardIndex+"'><u>剩余时间:"+ DateUtil.formatShowHMS(int(lefttime),true) +"</u></a>";
				TimerManager.instance.addFunction(timing);
			}
		}
		/**
		 *消除冷却时间 
		 * @param e
		 * 
		 */		
		private function dropTimeH(e :TextEvent):void
		{
			var curAwardIndex :int = int(e.text);
			var fcashs :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.fcashs;
			var needcash :int = fcashs[curAwardIndex - 1];
			var userCash :int = AppData.getInstance().clubDatamanager.clubInfo.cashpoint;
			var info :String;
			if(userCash < needcash)
			{
				info = "消耗"+needcash+"点卷可清除冷却时间。现有点券不足，是否前往充值?";
				Alert.noCashAlert(info);
				return ;
			}
			info = "消除冷却时间将花费"+ needcash +"点券，是否确认消除冷却时间?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,dropTimeAlertH);
		}
		/**
		 *消除冷却时间确认 
		 * @param type
		 * 
		 */		
		private function dropTimeAlertH(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new GoldPalaceEvent(GoldPalaceEvent.Drop_Time));
				txtAwardTime.mouseEnabled = false;
			}
		}
		/**
		 *领取奖励 
		 * @param e
		 * 
		 */		
		private function getAwardH(e :MouseEvent):void
		{
			this.dispatchEvent(new GoldPalaceEvent(GoldPalaceEvent.Get_Award));
			getBtn.mouseEnabled = false;
		}
		/**
		 * 点击球员 
		 * @param e
		 * 
		 */		
		private function pItemClickH(e :ListEvent):void
		{
			var pItem :SimplePlayerItem = e.target as SimplePlayerItem;
			selectPos = pItem.param;
			var pGoods :GoodsVo = pItem.data;
			var goodsUsePanel :GoodsUsePanel = GoodsUsePanel.inistance;
			goodsUsePanel.dragEnabled = true;
			goodsUsePanel.addEvtListener();
			goodsUsePanel.goodsList = AppData.getInstance().bagDataManager.getStarsGoodsByStarId(pGoods.gid);
			goodsUsePanel.showBtn(5);
			goodsUsePanel.callBack = playerItemUseH;
			var globPoint :Point = this.localToGlobal(new Point(180,108));
			PopUpManager.addWindow(goodsUsePanel,GlobalDef.GameWindowContainer,globPoint.x,globPoint.y,true);
		}
		/**
		 * 球员使用
		 * @param goodsVo
		 * 
		 */		
		private function playerItemUseH(goodsVo :GoodsVo):void
		{
			selectGoods = goodsVo;
			var info :String = "使用后，该球员将从背包里删除，是否确认使用?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,usePlayerAlertH);
		}
		/**
		 *使用球员确认 
		 * @param type
		 * 
		 */		
		private function usePlayerAlertH(type :int):void
		{
			if(type == Alert.YES)
			{
				if(GoodsUsePanel.inistance.parent)
				{
					GoodsUsePanel.inistance.parent.removeChild(GoodsUsePanel.inistance);
				}
				this.dispatchEvent(new GoldPalaceEvent(GoldPalaceEvent.Active_Player,{pid:selectGoods.pid,pos:selectPos}));
			}
		}
		/**
		 *改变奖励 
		 * @param e
		 * 
		 */		
		private function changeAwardH(e :MouseEvent):void
		{
			if(awardPanel.parent)
			{
				awardPanel.parent.removeChild(awardPanel);
			}
			else
			{
				_symbolSkin.addChild(awardPanel);
				awardPanel.x = 727;
				awardPanel.y = 186;
			}
		}
		/**
		 * onCloseHandler
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			changeBtn.removeEventListener(MouseEvent.CLICK,changeAwardH);
			txtAwardTime.removeEventListener(TextEvent.LINK,dropTimeH);
			getBtn.removeEventListener(MouseEvent.CLICK,getAwardH);
			var pSkin :Sprite;
			for(var i :int = 0;i<12;i++)
			{
				pSkin = _symbolSkin["player_" + i];
				pSkin.removeEventListener(ListEvent.ITEM_CLICK,pItemClickH);
			}
		}
		/**
		 * 获取信息 
		 * @param param
		 * 
		 */		
		public function getInfo(param:Object):void
		{
			initRes();
		}
		/**
		 * 激活球员 
		 * @param param
		 * 
		 */		
		public function activePlayer(param:Object):void
		{
			if(param.retcode == 0)
			{
				var pos :int = param.active;
				starMc["pMc_" + (pos-1)].gotoAndStop(2);
				var pSkin :Sprite = _symbolSkin["player_" + (pos-1)];
				pSkin.alpha = 1;
				pSkin.buttonMode = false;
				pSkin.removeEventListener(ListEvent.ITEM_CLICK,pItemClickH);
				var active :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.active;
				awardPanel.activePlayer(active);
				var awards :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.awards;
				comGoodItem.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(awards[active-1].type,awards[active-1].id);
				awardPanel.changeAward(active);
				refreshTime();
				motionMc["acM_"+(pos-1)].visible = true;
				var pItem :SimplePlayerItem = new SimplePlayerItem();
				pItem.initUI();
				pItem.data = selectGoods;
				DisplayObjectUtil.removeAllChildren(motionMc["acM_"+(pos-1)].pMc);
				motionMc["acM_"+(pos-1)].pMc.addChild(pItem);
				motionMc["acM_"+(pos-1)].gotoAndPlay(1);
				if(active == 12)
				{
					TweenLite.delayedCall(1.5,showGoldBg);
					if(finishMT)
					{
						_symbolSkin.addChild(finishMT);
						finishMT.gotoAndPlay(1);
						finishMT.visible = true;
					}
				}
			}
		}
		/**
		 * 更换奖励
		 * @param param
		 * 
		 */		
		public function changeAward(param:Object):void
		{
			if(param.retcode == 0)
			{
				var pos :int = param.pos;
				var awards :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.awards;
				comGoodItem.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(awards[pos-1].type,awards[pos-1].id);
				awardPanel.changeAward(pos);
				refreshTime();
			}
		}
		/**
		 *去除冷却时间 
		 * @param param
		 * 
		 */		
		public function dropTime(param:Object):void
		{
			txtAwardTime.mouseEnabled = true;
			if(param.retcode == 0)
			{
				getBtn.visible = true;
				txtAwardTime.visible = false;
				TimerManager.instance.removeFunction(timing);
			}
		}
		/**
		 * 领取奖励
		 * @param param
		 * 
		 */		
		public function getAward(param:Object):void
		{
			getBtn.mouseEnabled = true;
			if(param.retcode == 0)
			{
				refreshTime();
			}
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			DisplayObjectUtil.removeAllChildren(this);
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			for each(var pItem :SimplePlayerItem in itemlist)
			{
				pItem.destroyView();
				pItem = null;
			}
			itemlist = null;
			if(awardPanel)
			{
				awardPanel.destroyView();
				awardPanel = null;
			}
			if(comGoodItem)
			{
				comGoodItem.destroyView();
				comGoodItem = null;
			}
			TimerManager.instance.removeFunction(timing);
			selectGoods = null;
			DisplayObjectUtil.removeAllChildren(motionMc);
			motionMc = null;
			bgMc = null;
			finishMT = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 * 
		 */
		public function showType($obj:Object):void
		{
		}
	}
}